package com.weiaibenpao.demo.chislim.sport.utils;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Handler;
import android.os.Message;

import com.google.gson.Gson;
import com.weiaibenpao.demo.chislim.sport.bean.ProvinceBean;

import org.json.JSONArray;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

/**
 * Created by wlx on 2017/8/30.
 */

public class JsonProvinceUtils {
    private ArrayList<ProvinceBean> options1Items = new ArrayList<>();
    private ArrayList<ArrayList<String>> options2Items = new ArrayList<>();
    private ArrayList<ArrayList<ArrayList<String>>> options3Items = new ArrayList<>();
    private final int MSG_LOAD_FAILED=1;
    private static final int MSG_LOAD_SUCCESS = 0x0002;

    private Context context;

    public JsonProvinceUtils(Context context) {
        this.context = context;
    }

    public  ArrayList<ProvinceBean> getOptions1Items(){
        return options1Items;

    }
    public  ArrayList<ArrayList<String>> getOptions2Items(){
        return options2Items;

    }
    public ArrayList<ArrayList<ArrayList<String>>> getOptions3Items(){
        return options3Items;

    }
    private Handler mHandler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    };
    //数据解析
    public void initJsonData(){
        //获取assets文件夹下json的数据
        String json = getJson(context, "province.json");
        //用gson转换成实体
        ArrayList<ProvinceBean> provinceBeen = parseData(json);
        /**
         * 添加省级数据
         */
        options1Items=provinceBeen;
        for (int i = 0; i < provinceBeen.size(); i++) {//遍历省份
            ArrayList<String> cityList = new ArrayList<>();//该省份的城市列表(第二级)
            ArrayList<ArrayList<String>> province_areaList=new ArrayList<>();//该省份的所有的区（三级列表）

            for (int j = 0; j < provinceBeen.get(i).getCitylist().size(); j++) {

                String cityName = provinceBeen.get(i).getCitylist().get(j).getCityName();
                cityList.add(cityName);//添加城市
                ArrayList<String> city_areaList=new ArrayList<>();//该城市的所有地区
                //解析城市的地区
                /**
                 * 如果该地区无数据，添加一个空的，防止数据为null 导致三个选项长度不匹配造成崩溃
                 */
                if (provinceBeen.get(i).getCitylist().get(j).getAreaList() == null
                        ||provinceBeen.get(i).getCitylist().get(j).getAreaList().size()==0) {
                    city_areaList.add("");
                }else {
                    for (int k = 0; k < provinceBeen.get(i).getCitylist().get(j).getAreaList().size(); k++) {
                        ProvinceBean.AreaListBean areaListBean = provinceBeen.get(i).getCitylist().get(j).getAreaList().get(k);
                        city_areaList.add(areaListBean.getAreaName());//添加该城市的所有地区数据

                    }
                }
                province_areaList.add(city_areaList);//添加该城市所有地区数据
            }
            /**
             * 添加城市
             */

            options2Items.add(cityList);
            /**
             * 添加地区
             */
            options3Items.add(province_areaList);

        }
        mHandler.sendEmptyMessage(MSG_LOAD_SUCCESS);

    }
//    public void showPickerView(final TextView textView, final Polylines p){
//        OptionsPickerView pvOptions = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {
//            @Override
//            public void onOptionsSelect(int options1, int options2, int options3, View v) {
//                //返回的分别是三个级别的选中位置
//                String tx = options1Items.get(options1).getPickerViewText()+
//                        options2Items.get(options1).get(options2)+
//                        options3Items.get(options1).get(options2).get(options3);
//                textView.setText(tx);
//                p.drawLIne(options3Items.get(options1).get(options2).get(options3));
//                Log.e("wlx0303", "onOptionsSelect: "+options1Items.get(options1).getCitylist().get(options2).getCityPerimeter() );
//            }
//        }).setTitleText(" ")
//                .setDividerColor(Color.BLACK)
//                .setOutSideCancelable(true)
//                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
//                .setContentTextSize(20)
//                .setSubmitColor(Color.parseColor("#666666"))//确定按钮文字颜色
//                .setCancelColor(Color.parseColor("#333333"))//取消按钮文字颜色
//                .setLineSpacingMultiplier(2.0f)
//                .build();
//
//        /*pvOptions.setPicker(options1Items);//一级选择器
//        pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
//        pvOptions.setPicker(options1Items, options2Items,options3Items);//三级选择器
//        pvOptions.show();
//    }
    public ArrayList<ProvinceBean> parseData(String result) {//Gson 解析
        ArrayList<ProvinceBean> detail = new ArrayList<>();
        try {
            JSONArray data = new JSONArray(result);
            Gson gson = new Gson();
            for (int i = 0; i < data.length(); i++) {
                ProvinceBean entity = gson.fromJson(data.optJSONObject(i).toString(), ProvinceBean.class);
                detail.add(entity);
            }
        } catch (Exception e) {
            e.printStackTrace();
            mHandler.sendEmptyMessage(MSG_LOAD_FAILED);
        }
        return detail;
    }
    public String getJson(Context context, String fileName) {

        StringBuilder stringBuilder = new StringBuilder();
        try {
            AssetManager assetManager = context.getAssets();
            BufferedReader bf = new BufferedReader(new InputStreamReader(
                    assetManager.open(fileName)));
            String line;
            while ((line = bf.readLine()) != null) {
                stringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
}
